<template>
  <el-dialog :title="option.title" :visible.sync="option.isShow" width="60%" :close-on-click-modal="false"
    @open="openDialog" @close="closeDialog('ruleForm')">
    <el-row :gutter="10">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-col :span="13">
          <el-form-item label="日期" prop="DAY">
            <el-date-picker v-model="ruleForm.DAY" value-format="yyyyMMdd" size="small" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="是否节假日" prop="ISHOLIDAY">
            <el-select v-model="ruleForm.ISHOLIDAY" size="small" placeholder="请选择是否节假日">
              <el-option v-for="item in holidayOption" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="是否高峰日" prop="ISPEAKDAY">
            <el-select v-model="ruleForm.ISPEAKDAY" size="small" placeholder="请选择是否高峰日">
              <el-option v-for="item in ISPEAKDAYOption" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="13">
          <el-form-item label="数据是否有效" prop="ISVALID">
            <el-select v-model="ruleForm.ISVALID" size="small" filterable disabled clearable placeholder="请选择是否必输">
              <el-option v-for="item in valueOption" :key="item.key" :label="item.label" :value="item.key"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" size="mini" @click="submitForm('ruleForm')">保存</el-button>
            <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>
  import request from "@/common/utils/requestWFM";
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import dateManage from '@/message/wfm/FTEForecast/dateManage/dateManage'

  export default {
    props: {
      option: Object
    },
    computed: {
      isShow: {
        get: function () {
          return this.option.isShow;
        },
        set: function (newValue) {
          this.option.isShow = newValue;
        }
      }
    },
    mounted() {},
    data() {
      return {
        holidayOption: [{
            label: '节假日',
            value: 'Y'
          },
          {
            label: '非节假日',
            value: 'N',
          }
        ],
        ISPEAKDAYOption: [{
            label: '高峰日',
            value: 'Y'
          },
          {
            label: '非高峰日',
            value: 'N',
          }
        ],
        valueOption: [{
            label: '有效',
            value: 'Y'
          },
          {
            label: '无效',
            value: 'N',
          }
        ],
        ruleForm: {
          DAY: '', //日期
          ISHOLIDAY: 'N', //是否节假日
          ISPEAKDAY: 'N', //是否高峰日
          // ISVALID: 'Y', //数据是否有效
        },
        rules: {
          DAY: [{
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }]
        },
        loading: false,
      };
    },
    created() {

    },
    methods: {
      findName(arr) {
        let data = arr.filter(item => {
          return item.FIELDKEY == this.ruleForm.DBCOL
        })
        return data[0].FIELDVALUE
      },
      openDialog() {
        if (this.$refs['ruleForm'] != undefined) {
          this.$refs['ruleForm'].clearValidate();
        }
        if (this.option.flag == 'add') {
          this.ruleForm = {
            DAY: '', //日期
            ISHOLIDAY: 'N', //是否节假日
            ISPEAKDAY: 'N', //是否高峰日
            // ISVALID: 'Y', //数据是否有效
          }
        } else {
          this.ruleForm = JSON.parse(JSON.stringify(this.option.row));
        }
      },
      closeDialog(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].clearValidate();
        }
        // this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields();
      },
      //保存
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let resBody = new dateManage()
            resBody.data.OPERTYPE = this.option.flag == 'add' ? '01' : '02'
            resBody.data.DAY = this.ruleForm.DAY
            resBody.data.ISHOLIDAY = this.ruleForm.ISHOLIDAY
            resBody.data.ISPEAKDAY = this.ruleForm.ISPEAKDAY
            // resBody.data.ISVALID = this.ruleForm.ISVALID
            let response = await request(resBody);
            if (response.SYS_HEAD.ReturnCode == "000000") {
              if (this.option.flag == 'update') {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                })
              } else {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                })
              }
              this.isShow = false;
              this.$emit("refreshTable");
            } else {
              this.$message({
                type: "error",
                message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD
                  .ReturnMessage
              })
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].clearValidate();
          this.$refs[formName].resetFields();
        }
      }
    }
  };

</script>

<style scoped>

</style>
